package com.lenovo.vod.player.setting.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

public class LeEditText extends androidx.appcompat.widget.AppCompatEditText {

	private Rect mRect;
	private Paint mPaint;
	private int mCurrentSegment = 0;
	private int mCurrentNumIndex =0;
	private boolean mIsNum = true;
	private int mCurrentIndex = 0;
	private Handler mHandler = new Handler();
	
	public static final String KEY_HOUR  = "hour";
	public static final String KEY_MINUTE  = "minute";
	public static final String KEY_SECOND  = "second";
	
	public void init(){
		mCurrentSegment = 0;
		mIsNum = false;
		mCurrentIndex = 0;
		this.setTextValue("00:00:00");
		this.refreshDrawableState();
		this.setSelection(1);
		this.setNumIndex(1);
	}
	
	public void setSegment(int index){
		mCurrentSegment = index;
		this.refreshDrawableState();
	}
	
	public LeEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		mRect = new Rect();
		mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setColor(0x800000ff);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int count = getLineCount();
		Rect r = mRect;
		
		Paint pt = this.getPaint();
		int segmentWidth = (int)(0.5f+pt.measureText("00") );
		int dotWidth = (int)(0.5f+pt.measureText(":") );
		
		int charWidth = (int)(0.5f+pt.measureText("0"));
		int charTop = (int)(20f+pt.measureText("0"));
	
		for (int i = 0; i < count; i++) {

			int baseline = getLineBounds(i, r);

			baseline += 3;
			if(mCurrentSegment == 0){
				 canvas.drawLine(r.left, baseline , r.left+segmentWidth, baseline ,pt);
			}
			else if(mCurrentSegment == 1){
				 canvas.drawLine(r.left+segmentWidth+dotWidth, baseline,
						 r.left+2*segmentWidth+dotWidth, baseline ,pt);
			}
			else{
				canvas.drawLine(r.left+2*segmentWidth+2*dotWidth, baseline ,
						 r.left+3*segmentWidth+2*dotWidth, baseline ,pt);
			}
		}
		
		pt.setColor(Color.YELLOW);
		pt.setAlpha(100);
		switch(mCurrentNumIndex){
		case 0:
			canvas.drawRect(r.left, r.top, r.left+charWidth, r.top+charTop, pt);
			break;
		case 1:
			canvas.drawRect(r.left+charWidth, r.top, 
					r.left+2*charWidth, r.top+charTop, pt);
			break;
		case 3:
			canvas.drawRect(r.left+2*charWidth+dotWidth, r.top, 
					r.left+3*charWidth+dotWidth, r.top+charTop, pt);
			break;
		case 4:
			canvas.drawRect(r.left+3*charWidth+dotWidth, r.top, 
					r.left+4*charWidth+dotWidth, r.top+charTop, pt);
			break;
		case 6:
			canvas.drawRect(r.left+4*charWidth+2*dotWidth, r.top, 
					r.left+5*charWidth+2*dotWidth, r.top+charTop, pt);
			break;
		case 7:
			canvas.drawRect(r.left+5*charWidth+2*dotWidth, r.top, 
					r.left+6*charWidth+2*dotWidth, r.top+charTop, pt);
			break;
		}
	}
	public static interface TextListener {
		
		public void onEnterDown(String value);
		public void onBackDown(String value);
		
	}
	private TextListener mTextListener ;
	public void setTextListener(TextListener listener){
		mTextListener = listener;
	}
	public void setTextValue(String value){
		this.removeTextChangedListener(mTextWatcher);
		super.setText(value);
		
		this.addTextChangedListener(mTextWatcher);
	}
	
	public void setValue(String key, String value){
		this.removeTextChangedListener(mTextWatcher);
		if(value.length()>2) return;	
		if(value.length()==1) value = "0"+value;
		
		String innerText = this.getText().toString();
		if(key.equals(KEY_HOUR)){
			innerText = value + innerText.substring(2);
			this.setText(innerText);
		}
		else if(key.equals(KEY_MINUTE)){
			String prex = innerText.substring(0, innerText.indexOf(":")+1);			
			String tail = innerText.substring(innerText.lastIndexOf(":"));
			this.setText(prex+value+tail);
		}
		else if(key.equals(KEY_SECOND)){

			innerText = innerText.substring(0, innerText.lastIndexOf(":")+1);
			innerText += value;
			this.setText(innerText);
		}
		this.addTextChangedListener(mTextWatcher);
		
	}
	
	
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		Editable etext = LeEditText.this.getText();
		Selection.setSelection(etext, 1);
		setNumIndex(1);
		this.addTextChangedListener(mTextWatcher);
		
		this.setOnKeyListener(new OnKeyListener(){

			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				
				int action = event.getAction();
				Editable etext = LeEditText.this.getText();
				int sta = LeEditText.this.getSelectionStart();
				mCurrentIndex = sta;
				
				switch(keyCode){
				case KeyEvent.KEYCODE_DEL:
					return true;
				case KeyEvent.KEYCODE_SPACE:
					return true;
				}	
				
				if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
						&& action==KeyEvent.ACTION_DOWN 
						&& event.getRepeatCount()==0
						){	
					if(mIsNum){
						if(sta<2) {
							Selection.setSelection(etext, 2);	
							setNumIndex(2+1);
						}else if(sta<5) {
							Selection.setSelection(etext, 5);
							setNumIndex(5+1);
						}
					}else{
						if(sta<2) {
							Selection.setSelection(etext, 3);
							setNumIndex(3+1);
						}else if(sta<5) {
							Selection.setSelection(etext, 6);
							setNumIndex(6+1);
						}else if(sta >=7){
							Selection.setSelection(etext, sta -1);
							setNumIndex(sta );
						}
					}
					setSegment();
				}else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT 
						&& action==KeyEvent.ACTION_DOWN 
						&& event.getRepeatCount()==0
						){
					if(mIsNum){
						if(sta >= 6) {
							Selection.setSelection(etext, 4);
							setNumIndex(3);
						}else if(sta >= 3) {
							Selection.setSelection(etext, 0);
							setNumIndex(0);
						}
					}else{
						if(sta >= 6) {
							Selection.setSelection(etext, 5);	
							setNumIndex(4);
						}else if(sta >= 3) {
							Selection.setSelection(etext, 2);
							setNumIndex(1);
						}else if(sta >=1){
							Selection.setSelection(etext, sta+1);
							setNumIndex(sta);
						}
					}
					setSegment();
				}else if(keyCode >= KeyEvent.KEYCODE_0
						&& keyCode <= KeyEvent.KEYCODE_9
						&& action==KeyEvent.ACTION_DOWN 
						&& event.getRepeatCount()==0
						){	
					if(sta>=8) return true;
					if(!mIsNum){
						if(sta == 1){
							sta = 0;
							Selection.setSelection(etext, 0);
							setNumIndex(0);
						}else if(sta == 4){
							sta = 3;
							Selection.setSelection(etext, 3);
							setNumIndex(3);
						}else if(sta == 7){
							sta = 6;
							Selection.setSelection(etext, 6);
							setNumIndex(6);
						}
					}
					mIsNum = true;
					String tmp = etext.toString();
					String prex = tmp.substring(0, sta);
					String tail = tmp.substring(sta+1);
					String str = prex+ (keyCode-KeyEvent.KEYCODE_0) + tail;
					
					return !checkValid(str);
				}else if(keyCode >= KeyEvent.KEYCODE_NUMPAD_0
						&& keyCode <= KeyEvent.KEYCODE_NUMPAD_9
						&& action==KeyEvent.ACTION_DOWN 
						&& event.getRepeatCount()==0){
					if(sta>=8) return true;
					if(!mIsNum){
						if(sta == 1){
							sta = 0;
							Selection.setSelection(etext, 0);
							setNumIndex(0);
						}else if(sta == 4){
							sta = 3;
							Selection.setSelection(etext, 3);
							setNumIndex(3);
						}else if(sta == 7){
							sta = 6;
							Selection.setSelection(etext, 6);
							setNumIndex(6);
						}
					}
					mIsNum = true;
					String tmp = etext.toString();
					String prex = tmp.substring(0, sta);
					String tail = tmp.substring(sta+1);
					String str = prex+ (keyCode-KeyEvent.KEYCODE_NUMPAD_0) + tail;
					return !checkValid(str);
				}else if(keyCode == KeyEvent.KEYCODE_DPAD_UP
						&& action==KeyEvent.ACTION_DOWN 
						&& event.getRepeatCount()==0
						){	
					if(mIsNum){
						if(sta == 0){
							sta = 1;
							Selection.setSelection(etext, 1);
							setNumIndex(2);
						}else if(sta == 3){
							sta = 4;
							Selection.setSelection(etext, 4);
							setNumIndex(5);
						}else if(sta == 6){
							sta = 7;
							Selection.setSelection(etext, 7);
							setNumIndex(8);
						}
					}
					mIsNum = false;
					if(sta>=8) return true;
					String tmp = etext.toString();
					String hourStr = tmp.substring(0,2);
					String minuteStr = tmp.substring(3,5);
					String secondStr = tmp.substring(6,8);
					
					String currentText = tmp.substring(sta,sta+1);
					if(sta <=0){
						return true;
					}
					String prexText = tmp.substring(sta-1,sta);
					int currentNum = Integer.parseInt(currentText);
					int prexNum = Integer.parseInt(prexText);
					String str = null;
					
					if(sta == 1){
						if(currentNum == 9 && prexNum <= 8 ){
							str = (prexNum +1) + "0" +":"+minuteStr +":"+ secondStr;
						}else if(currentNum <= 8){
							str = prexText + (currentNum + 1) +":"+minuteStr +":"+ secondStr;
						}else {
							return true;
						}
					}else if(sta == 4){
						if(currentNum == 9 && prexNum <= 8 ){
							str = hourStr + ":" + (prexNum +1) + "0" +":"+ secondStr;
						}else if(currentNum <= 8){
							str =  hourStr + ":" + prexText + (currentNum + 1) +":"+ secondStr;
						}else {
							return true;
						}
					}else if(sta == 7){
						if(currentNum == 9 && prexNum <= 8 ){
							str = hourStr + ":" + minuteStr +":"+ (prexNum +1) + "0";
						}else if(currentNum <= 8){
							str =  hourStr + ":" + minuteStr +":"+ prexText + (currentNum + 1);
						}else {
							return true;
						}
					}
					if(checkValid(str)){
						LeEditText.this.setText(str);
						return false;
					}else{
						return true;
					}
				}
				else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN
						&& action==KeyEvent.ACTION_DOWN 
						&& event.getRepeatCount()==0
						){	
					if(mIsNum){
						if(sta == 0){
							sta = 1;
							Selection.setSelection(etext, 1);
						}else if(sta == 3){
							sta = 4;
							Selection.setSelection(etext, 4);
						}else if(sta == 6){
							sta = 7;
							Selection.setSelection(etext, 7);
						}
					}
					mIsNum = false;
					if(sta>=8) return true;
					String tmp = etext.toString();
					String hourStr = tmp.substring(0,2);
					String minuteStr = tmp.substring(3,5);
					String secondStr = tmp.substring(6,8);
					
					String currentText = tmp.substring(sta,sta+1);
					if(sta <=0){
						return true;
					}
					String prexText = tmp.substring(sta-1,sta);
					int currentNum = Integer.parseInt(currentText);
					int prexNum = Integer.parseInt(prexText);
					String str = null;
					
					if(sta == 1){
						if(currentNum == 0 && prexNum != 0 ){
							str = (prexNum - 1) + "9" +":"+minuteStr +":"+ secondStr;
						}else if(currentNum > 0){
							str = prexText + (currentNum - 1) +":"+minuteStr +":"+ secondStr;
						}else {
							return true;
						}
					}else if(sta == 4){
						if(currentNum == 0 && prexNum != 0 ){
							str = hourStr + ":" + (prexNum - 1) + "9" +":"+ secondStr;
						}else if(currentNum > 0){
							str =  hourStr + ":" + prexText + (currentNum - 1) +":"+ secondStr;
						}else {
							return true;
						}
					}else if(sta == 7){
						if(currentNum == 0 && prexNum != 0  ){
							str = hourStr + ":" + minuteStr +":"+ (prexNum - 1) + "9";
						}else if(currentNum > 0){
							str =  hourStr + ":" + minuteStr +":"+ prexText + (currentNum - 1);
						}else {
							return true;
						}
					}
					if(checkValid(str)){
						LeEditText.this.setText(str);
						return false;
					}else{
						return true;
					}
				}else if(keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z 
						&& action==KeyEvent.ACTION_UP 
						&& event.getRepeatCount()==0){
					if(keyCode==KeyEvent.KEYCODE_B || keyCode == KeyEvent.KEYCODE_S){
						if(mTextListener!=null){
							mTextListener.onBackDown(etext.toString());
						}
						return true;
					}
					return true;
				}else if(keyCode == KeyEvent.KEYCODE_ENTER						
						&& action==KeyEvent.ACTION_DOWN 
						&& event.getRepeatCount()==0
						){
					String tmp = etext.toString();
					if(mTextListener!=null)
						mTextListener.onEnterDown(tmp);
				}
			
				return false;
			}});
	}
	private boolean checkValid(String value){
		
		if(value != null && !"".equals(value)){
			String tmp = value.substring(3, 5);
			if(Integer.parseInt(tmp)>=60) return false;
			
			tmp = value.substring(6, 8);
			if(Integer.parseInt(tmp)>=60) return false;
		}
		
		return true;
	}
		
	private TextWatcher mTextWatcher = new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			
			final int sta = LeEditText.this.getSelectionStart();
			if (sta<=8 ) {
				
				final TextWatcher watcher = this;
				LeEditText.this.removeTextChangedListener(watcher);
				if(mIsNum){
					s.delete(sta, sta+1);					
					LeEditText.this.setText(s);
					mHandler.post(new Runnable(){
						public void run() {
							final Editable etext = LeEditText.this.getText();
							if (sta==2  ){	
								Selection.setSelection(etext, sta +1);	
								setNumIndex(sta+1);
							}
							else if(sta == 5){
								Selection.setSelection(etext, sta +1);								
								setNumIndex(sta+1);
							}
							else if(sta == 8){
								Selection.setSelection(etext, sta -2);								
								setNumIndex(sta-2);
							}
							else{
								Selection.setSelection(etext, sta );
								setNumIndex(sta);
							}
							setSegment();
							LeEditText.this.addTextChangedListener(watcher);
						}});
				}else{
					mHandler.post(new Runnable(){
						public void run() {
							final Editable etext = LeEditText.this.getText();
							switch(mCurrentIndex){
							case 0:
							case 3:
							case 6:
								Selection.setSelection(etext, mCurrentIndex+1);	
								setNumIndex(mCurrentIndex+1);
								break;
							case 2:
							case 5:
								Selection.setSelection(etext, mCurrentIndex+2);	
								setNumIndex(mCurrentIndex+2);
								break;
							default:
								Selection.setSelection(etext, mCurrentIndex);
								setNumIndex(mCurrentIndex);
							}
							LeEditText.this.addTextChangedListener(watcher);
						}});
				}
			}
			else{
				final TextWatcher watcher = this;
				LeEditText.this.removeTextChangedListener(watcher);
				if(mIsNum){
					s.delete(8, 9);					
					LeEditText.this.setText(s);
				}
				mHandler.post(new Runnable(){
					public void run() {
						final Editable etext = LeEditText.this.getText();						
						Selection.setSelection(etext, 8 );						
					
						LeEditText.this.addTextChangedListener(watcher);
					}});
			}
		}
	};
	
	private void setSegment(){
		int sta = LeEditText.this.getSelectionStart();
		if(mIsNum){
			if(sta<2){
				mCurrentSegment = 0;
			}else if(sta<5){
				mCurrentSegment = 1;
			}else{
				mCurrentSegment = 2;
			}
		}else{
			if(sta<=2){
				mCurrentSegment = 0;
			}else if(sta<=5){
				mCurrentSegment = 1;
			}else{
				mCurrentSegment = 2;
			}
		}
		
	}
	public void setNumIndex(int index){
		mCurrentNumIndex = index;
	}
}
